package Views;

import Controllers.CarController;
import Models.Car;
import Models.EmptyValueException;
import java.awt.event.WindowEvent;
import javax.swing.event.ListSelectionEvent;

public class CarPanel extends ViewAbstract<Models.Car>
{
    public CarPanel(SearchableTabbedPane parent)
	{
        initComponents();
		this.controller = new CarController();
		this.tabbedPane = parent;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Sep2 = new javax.swing.JSeparator();
        showAllB = new javax.swing.JButton();
        newB = new javax.swing.JButton();
        editB = new javax.swing.JButton();
        deleteB = new javax.swing.JButton();
        cancelB = new javax.swing.JButton();
        saveB = new javax.swing.JButton();
        searchB = new javax.swing.JButton();
        carNoL = new javax.swing.JLabel();
        carNoTF = new javax.swing.JTextField();
        licensePlateL = new javax.swing.JLabel();
        licensePlateTF = new javax.swing.JTextField();
        searchTF = new javax.swing.JTextField();
        Sep1 = new javax.swing.JSeparator();
        Sep3 = new javax.swing.JSeparator();

        setMaximumSize(new java.awt.Dimension(600, 420));
        setMinimumSize(new java.awt.Dimension(600, 420));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(600, 420));

        Sep2.setName("Sep2"); // NOI18N

        showAllB.setText("Vis Alle");
        showAllB.setName("showAllB"); // NOI18N
        showAllB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllBActionPerformed(evt);
            }
        });

        newB.setText("Ny");
        newB.setName("newB"); // NOI18N
        newB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newBActionPerformed(evt);
            }
        });

        editB.setText("Rediger");
        editB.setEnabled(false);
        editB.setName("editB"); // NOI18N
        editB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editBActionPerformed(evt);
            }
        });

        deleteB.setText("Slet");
        deleteB.setEnabled(false);
        deleteB.setName("deleteB"); // NOI18N
        deleteB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBActionPerformed(evt);
            }
        });

        cancelB.setText("Annullér");
        cancelB.setEnabled(false);
        cancelB.setName("cancelB"); // NOI18N
        cancelB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBActionPerformed(evt);
            }
        });

        saveB.setText("Gem");
        saveB.setEnabled(false);
        saveB.setName("saveB"); // NOI18N
        saveB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBActionPerformed(evt);
            }
        });

        searchB.setText("Søg");
        searchB.setName("searchB"); // NOI18N
        searchB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBActionPerformed(evt);
            }
        });

        carNoL.setText("BilNo.:");
        carNoL.setName("carNoL"); // NOI18N

        carNoTF.setEnabled(false);
        carNoTF.setMaximumSize(new java.awt.Dimension(200, 20));
        carNoTF.setMinimumSize(new java.awt.Dimension(200, 20));
        carNoTF.setName("carNoTF"); // NOI18N
        carNoTF.setPreferredSize(new java.awt.Dimension(200, 20));

        licensePlateL.setText("NoPlade.:");
        licensePlateL.setName("licensePlateL"); // NOI18N

        licensePlateTF.setEnabled(false);
        licensePlateTF.setMaximumSize(new java.awt.Dimension(200, 20));
        licensePlateTF.setMinimumSize(new java.awt.Dimension(200, 20));
        licensePlateTF.setName("licensePlateTF"); // NOI18N
        licensePlateTF.setPreferredSize(new java.awt.Dimension(200, 20));

        searchTF.setMaximumSize(new java.awt.Dimension(200, 20));
        searchTF.setMinimumSize(new java.awt.Dimension(200, 20));
        searchTF.setName("searchTF"); // NOI18N
        searchTF.setPreferredSize(new java.awt.Dimension(200, 20));

        Sep1.setName("Sep1"); // NOI18N

        Sep3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        Sep3.setName("Sep3"); // NOI18N
        Sep3.setPreferredSize(new java.awt.Dimension(5, 23));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(licensePlateL)
                            .addComponent(carNoL))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(carNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(licensePlateTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchB))
                            .addComponent(Sep1)))
                    .addComponent(Sep2, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(showAllB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 187, Short.MAX_VALUE)
                        .addComponent(cancelB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveB)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(carNoL)
                            .addComponent(carNoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(licensePlateL)
                            .addComponent(licensePlateTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(searchB)
                            .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Sep1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 319, Short.MAX_VALUE)
                .addComponent(Sep2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(showAllB)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(saveB)
                        .addComponent(cancelB)
                        .addComponent(newB)
                        .addComponent(editB)
                        .addComponent(deleteB))
                    .addComponent(Sep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	private void newBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newBActionPerformed
		clearComponents();
		changeComponentState(true);
		newOne = new Car();
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_newBActionPerformed

	private void editBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editBActionPerformed
		changeComponentState(true);
		newB.setEnabled(false);
		editB.setEnabled(false);
	}//GEN-LAST:event_editBActionPerformed

	private void deleteBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBActionPerformed
		if(confirmDeletion("bilen"))
		{
			controller.delete(chosenOne.getCarNo());
			clearComponents();
		}
	}//GEN-LAST:event_deleteBActionPerformed

	private void cancelBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBActionPerformed
		clearComponents();
		changeComponentState(false);
	}//GEN-LAST:event_cancelBActionPerformed

	private void saveBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBActionPerformed
		Car theOne = getTheOne();

		try
		{
			theOne.setData(licensePlateTF.getText());

			if(chosenOne == null)
			{
				controller.insert(theOne);
			}
			else
			{
				controller.update(theOne, "carNo = "+theOne.getCarNo());
			}
		}
		catch (EmptyValueException e)
		{
			informAboutEmptyFields();
			return;
		}

		informSavingWentOk("Bilen");
		clearComponents();
		changeComponentState(false);
		newB.setEnabled(true);
	}//GEN-LAST:event_saveBActionPerformed

	private void searchBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBActionPerformed
		tabbedPane.search(TabIndex.Cars, searchTF.getText());
	}//GEN-LAST:event_searchBActionPerformed
	
	private void showAllBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllBActionPerformed
		tabbedPane.search(TabIndex.Cars, "*");
	}//GEN-LAST:event_showAllBActionPerformed
	
	protected void changeComponentState(boolean state)
	{
		licensePlateTF.setEnabled(state);
		cancelB.setEnabled(state);
		saveB.setEnabled(state);
		setUnsavedState(state);
	}

	protected void populateFields()
	{
		carNoTF.setText(String.valueOf(chosenOne.getCarNo()));
		licensePlateTF.setText(String.valueOf(chosenOne.getLicensePlate()));
		newB.setEnabled(true);
		editB.setEnabled(true);
		deleteB.setEnabled(true);
	}

	protected void clearComponents()
	{
		chosenOne = null;
		newOne = null;
		carNoTF.setText("");
		licensePlateTF.setText("");
		newB.setEnabled(true);
		editB.setEnabled(false);
		deleteB.setEnabled(false);
	}

	protected void showNo(int no)
	{
		chosenOne = controller.find(no);
		populateFields();
	}

	public void valueChanged(ListSelectionEvent e) {}
	public void windowClosing(WindowEvent e) {}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator Sep1;
    private javax.swing.JSeparator Sep2;
    private javax.swing.JSeparator Sep3;
    private javax.swing.JButton cancelB;
    private javax.swing.JLabel carNoL;
    private javax.swing.JTextField carNoTF;
    private javax.swing.JButton deleteB;
    private javax.swing.JButton editB;
    private javax.swing.JLabel licensePlateL;
    private javax.swing.JTextField licensePlateTF;
    private javax.swing.JButton newB;
    private javax.swing.JButton saveB;
    private javax.swing.JButton searchB;
    private javax.swing.JTextField searchTF;
    private javax.swing.JButton showAllB;
    // End of variables declaration//GEN-END:variables
}